package com.chinasoft.reposearch.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.chinasoft.reposearch.entity.po.LanguageCount;
import com.chinasoft.reposearch.entity.po.Repo;
import com.chinasoft.reposearch.entity.po.TopicCount;
import com.chinasoft.reposearch.mapper.LanguageCountMapper;
import com.chinasoft.reposearch.mapper.TopicCountMapper;
import com.chinasoft.reposearch.service.DataCountService;
import com.chinasoft.reposearch.service.HiveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class DataCountServiceImpl implements DataCountService {
    private final HiveService hiveService;
    private final LanguageCountMapper languageCountMapper;
    private final TopicCountMapper topicCountMapper;

    @Autowired
    public DataCountServiceImpl(HiveService hiveService, LanguageCountMapper languageCountMapper, TopicCountMapper topicCountMapper) {
        this.hiveService = hiveService;
        this.languageCountMapper = languageCountMapper;
        this.topicCountMapper = topicCountMapper;
    }

    @Override
    public void countLanguage() {
        hiveService.executeSqlFile("sql/count_language.sql");
    }

    @Override
    public void countTopic() {
        hiveService.executeSqlFile("sql/count_topic.sql");
    }

    @Override
    public void dumpToMysql() {
        List<Map<String, Object>> languageCountResults = hiveService.queryForList(new String[]{"language", "count", "year", "month"}, "repo_search.language_count");
        List<LanguageCount> languageCounts = new ArrayList<>();
        for (Map<String, Object> languageCountResult : languageCountResults) {
            String language = (String) languageCountResult.get("language");
            Integer count = (Integer) languageCountResult.get("count");
            Integer year = (Integer) languageCountResult.get("year");
            Integer month = (Integer) languageCountResult.get("month");
            languageCounts.add(new LanguageCount(null, language, count, year, month));
        }
        languageCountMapper.delete(new QueryWrapper<>());
        languageCountMapper.insert(languageCounts);

        List<Map<String, Object>> topicCountResults = hiveService.queryForList(new String[]{"topic", "count", "year", "month"}, "repo_search.topic_count");
        List<TopicCount> topicCounts = new ArrayList<>();
        for (Map<String, Object> topicCountResult : topicCountResults) {
            String topic = (String) topicCountResult.get("topic");
            Integer count = (Integer) topicCountResult.get("count");
            Integer year = (Integer) topicCountResult.get("year");
            Integer month = (Integer) topicCountResult.get("month");
            topicCounts.add(new TopicCount(null, topic, count, year, month));
        }
        topicCountMapper.delete(new QueryWrapper<>());
        topicCountMapper.insert(topicCounts);
    }
}
